/**
 * jims
 */
package com.jims.master.modules.csm.web;

import com.jims.master.common.persistence.Page;
import com.jims.master.common.utils.StringUtils;
import com.jims.master.common.web.BaseController;
import com.jims.master.modules.csm.dao.ExpClassDictDao;
import com.jims.master.modules.csm.entity.*;
import com.jims.master.modules.csm.service.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 消耗理信息浏览器Controller
 * @author DT
 * @version 2017-03-15
 */
@Controller
@RequestMapping(value = "${adminPath}/csm/expInfoBrowser")
public class ExpInfoBrowserController extends BaseController {

	@Autowired
	private ExpNameDictService expNameDictService;
	@Autowired
	private ExpStockService expStockService;
	@Autowired
	private ExpDetailsService expDetailsService;
	@Autowired
	private ExpInfoService expInfoService;
	@Autowired
	private ExpClassDictDao expClassDictDao;
	@ModelAttribute
	public ExpNameDict get(@RequestParam(required=false) String id) {
		ExpNameDict entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = expNameDictService.get(id);
		}
		if (entity == null){
			entity = new ExpNameDict();
		}
		return entity;
	}

	@RequiresPermissions("user")
	@RequestMapping(value = {"index", ""})
	public String index(ExpInfo expInfo,HttpServletRequest request, HttpServletResponse response, Model model) {
		if(expInfo.getExpDict()!=null){
            ExpDict expDict=new ExpDict();
            if(expInfo.getExpDict().getExpClassID()!=null && !"".equals(expInfo.getExpDict().getExpClassID()) ){
                ExpClassDict expClassDict = expClassDictDao.get(expInfo.getExpDict().getExpClassID());
                expDict.setExpCode(expClassDict.getClassCode());
            }
			expDict.setExpName(expInfo.getExpDict().getExpName());
			expInfo.setExpDict(expDict);
		}
		List<ExpNameDict> list=expNameDictService.getExpNameList(new ExpNameDict());
		model.addAttribute("list",list);
		Page<ExpInfo> page=expInfoService.findExpInfoList(new Page<ExpInfo>(request,response),expInfo);
		model.addAttribute("page",page);
		model.addAttribute("expInfo",expInfo);
		return "modules/csm/expInfoBrowser";
	}
	@RequiresPermissions("user")
	@RequestMapping(value = {"dialog", ""})
	public String dialog(ExpDetails expDetails, HttpServletRequest request, HttpServletResponse response, Model model) {
		expDetails=expDetailsService.findByexpInfoId(expDetails);
		model.addAttribute("expDetails", expDetails);
		return "modules/csm/expDetailsInfo";
	}
	@RequiresPermissions("user")
	@RequestMapping(value = {"stockDetails", ""})
	public String stockDetails(ExpStock expStock, HttpServletRequest request, HttpServletResponse response, Model model) {
		expStock = expStockService.findExpByInfoId(expStock);
		model.addAttribute("expStock", expStock);
		return "modules/csm/expStockDetails";
	}

}